/*
 * @Author: lll 1280388921@qq.com
 * @Date: 2025-05-29 14:45:37
 * @LastEditors: lll 1280388921@qq.com
 * @LastEditTime: 2025-05-29 23:17:05
 * @FilePath: /lesson41/udp/UdpServer.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once

#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <string>
#include <cstring>
#include <strings.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include<functional>
#include "log.hpp"

// using func_t =function<string(const string&)>;

typedef function<string(const string&)> func_t;

Log lg;

using namespace std;

enum
{
    SOCKET_ERR = 1,
    BIND_ERR
};

uint16_t defaultport = 8080;
string defaultip = "0.0.0.0";

class UdpServer
{
public:
    UdpServer(const uint16_t &port = defaultport, const string &ip = defaultip)
        : socketfd_(0), port_(port), ip_(ip), isrunning_(false)
    {
    }
    void Init()
    {
        // 1.创建udp socket
        socketfd_ = socket(AF_INET, SOCK_DGRAM, 0); // PF_INET
        if (socketfd_ < 0)
        {
            lg(Fatal, "socket create error,socketfd: %d", socketfd_);
            exit(SOCKET_ERR);
        }
        lg(Info, "socket create success,socketfd: %d", socketfd_);

        // 2.bind socket
        struct sockaddr_in local;
        bzero(&local, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = htons(port_);                  // 要保证端口号是网络字节序列，因为该端口号是要给对方发送的。
        local.sin_addr.s_addr = inet_addr(ip_.c_str()); // 1.string -> uint32_t 2.要是网络序列的
        // local.sin_addr.s_addr=INADDR_ANY;

        if (bind(socketfd_, (const struct sockaddr *)&local, sizeof(local)) < 0)
        {
            lg(Fatal, "bind error,errno: %d,err string: %s\n", errno, strerror(errno));
            exit(BIND_ERR);
        }
        lg(Info, "bind success,errno: %d,err string: %s\n", errno, strerror(errno));
    }
    void Run(func_t ft)
    {
        isrunning_ = true;
        char inbuffer[1024];
        while (isrunning_)
        {
            struct sockaddr_in client;
            socklen_t len = sizeof(client);
            ssize_t n = recvfrom(socketfd_, inbuffer, sizeof(inbuffer) - 1, 0, (struct sockaddr *)&client, &len);
            if (n < 0)
            {
                lg(Warming, "recvfrom error,errno: %d,err string: %s\n", errno, strerror(errno));
                continue;
            }
            inbuffer[n] = 0;
            string info = inbuffer;
            // string echo_string = "server echo : " + info;
            // cout<<echo_string<<endl;
            string echo_string=ft(info);

            sendto(socketfd_, echo_string.c_str(), echo_string.size(), 0, (struct sockaddr *)&client, len);
        }
    }
    ~UdpServer()
    {
        if (socketfd_ > 0)
        {
            close(socketfd_);
        }
    }

private:
    int socketfd_;  // 网络文件描述符
    string ip_;     // 任意地址绑定 0
    uint16_t port_; // 表明服务器进程的端口号
    bool isrunning_;
};
